//
//  TKBidSheetView.m
//  Ticket
//
//  Created by 小禹宝宝 on 2017/10/25.
//  Copyright © 2017年 cy55. All rights reserved.
//

#import "TKBidSheetView.h"
#import "NSString+JCKit.h"

#define bgHeight mScreenHeight/1.7
#define botlineY 93
#define bidBtnX 15
#define bidBtnHeight 30
#define margin 10
@interface TKBidSheetView ()
@property (nonatomic, copy)TKActionBlock priceBlock;

@property (nonatomic, strong) UIView *shadeView;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UILabel *priceLeadLabel;
@property (nonatomic, strong) UITextField *bidTextField;

@end
@implementation TKBidSheetView

- (instancetype)init
{
    self = [super init];
    if (self) {
        UIWindow *window = [[UIApplication sharedApplication]keyWindow];
        
        self.frame = CGRectMake(0, 0 , mScreenWidth, mScreenHeight);
        
        [window addSubview:self];
        self.hidden = YES;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    //
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    [topView setBackgroundColor:[UIColor jc_colorWithHexString:@"121212" alpha:0.3]];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [topView addGestureRecognizer:tap];
    [self addSubview:topView];
    self.shadeView = topView;
    //
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height, self.bounds.size.width, bgHeight)];
    bgView.backgroundColor = [UIColor jc_colorWithHexString:@"efefef" alpha:1];
//    bgView.backgroundColor = [UIColor whiteColor];
    [bgView setAlpha:1];
    [self addSubview:bgView];
    self.backgroundView = bgView;
    
    CGFloat bidBtnH = bidBtnHeight;
    CGFloat bidBtnmargin = bidBtnX;
    

    
    UIView *leadingView = [UIView new];
    [bgView addSubview:leadingView];
    leadingView.backgroundColor = [BLColorConfig backgroundColor];
    leadingView.frame = CGRectMake(bidBtnmargin, margin, mScreenWidth-(bidBtnmargin*2), bidBtnH);
    UILabel *leadLabel = [UILabel new];
    leadLabel.text = @"领先价";
    leadLabel.textColor = [BLColorConfig textColor];
    _priceLeadLabel = [UILabel new];
    _priceLeadLabel.text = @"123213213";
    _priceLeadLabel.textColor = [BLColorConfig mainColor];
    [leadingView addSubview:leadLabel];
    [leadingView addSubview:_priceLeadLabel];
    [leadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.leading.mas_equalTo(0);
        make.height.mas_equalTo(20);
    }];
    [_priceLeadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.leading.mas_equalTo(leadLabel.mas_trailing).mas_offset(15);
        make.height.mas_equalTo(20);
    }];

    UIView *chujiaView = [UIView new];
    [bgView addSubview:chujiaView];
    
    chujiaView.backgroundColor = [UIColor whiteColor];
    chujiaView.frame = CGRectMake(bidBtnmargin, 60, mScreenWidth-(bidBtnmargin*2), bidBtnH);
    UILabel *chujiaLabel = [UILabel new];
    chujiaLabel.text = @"出价";
    chujiaLabel.textColor = [BLColorConfig textColor];
    _bidTextField = [UITextField new];

    _bidTextField.textColor = [BLColorConfig mainColor];
    [chujiaView addSubview:chujiaLabel];
    [chujiaView addSubview:_bidTextField];
    [chujiaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.leading.mas_equalTo(0);
        make.height.mas_equalTo(20);
    }];
    [_bidTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.leading.mas_equalTo(chujiaLabel.mas_trailing).mas_offset(15);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(120);

    }];
    
    UIButton *bidBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [bidBtn setFrame:CGRectMake(bidBtnmargin, bgHeight - bidBtnH - margin, mScreenWidth-(bidBtnmargin*2), bidBtnH)];
    [bidBtn setTitle:@"出价" forState:UIControlStateNormal];
    [bidBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bidBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [bidBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    bidBtn.layer.cornerRadius = 6.0;
    [bidBtn setBackgroundColor:[BLColorConfig mainColor]];
    [bidBtn addTarget:self action:@selector(bidBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:bidBtn];
    
    [bidBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(115);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(bidBtnH);
        make.width.mas_equalTo(mScreenWidth-(bidBtnmargin*2));
        
    }];
}


- (void)bidBtnClick:(UIButton *)btn
{
    if (self.priceBlock) {
        self.priceBlock(self.bidTextField.text);
    }
    [self dismiss];

}


- (void)bidClicked:(TKActionBlock)priceBlock {
   self.priceBlock = priceBlock;
}



- (void)tap:(UITapGestureRecognizer *)tap
{
    [self dismiss];
}

- (void)show
{
    if ([self.top_price jc_isValidString]) {
        _priceLeadLabel.text = self.top_price;
    }
    if ([self.bid_price jc_isValidString]) {
        _bidTextField.text = self.bid_price;
    }

    UIWindow *window = [[UIApplication sharedApplication]keyWindow];
    [window addSubview:self];
    
    [UIView animateWithDuration:0.3 animations:^{
        // 设置整个view弹出来以后的位置（最终位置）
        self.shadeView.alpha = 0.5f;
        self.hidden = NO;
        self.backgroundView.frame = CGRectMake(0, self.bounds.size.height-bgHeight, self.bounds.size.width, bgHeight);
        
    }completion:^(BOOL finished) {
        self.shadeView.alpha = 1.0f;
        
    }];
    
}

- (void)dismiss
{
    
    [UIView animateWithDuration:0.3f animations:^{
        //整个View的初始位置，
        self.shadeView.alpha = 0.1f;
        self.backgroundView.frame = CGRectMake(0, self.bounds.size.height, self.bounds.size.width, bgHeight);
    } completion:^(BOOL finished) {
        self.shadeView.alpha = 0.f;
        self.hidden = YES;
        [self removeFromSuperview];
        
    }];
}
@end
